<template>
  <div class="baleTable">
    <div class="baleTable-head">
      <div class="head-left">
        <slot name="headLeft"></slot>
      </div>
      <div class="head-right">
        <!-- 列表显示与隐藏组件 -->
        <tableColumnSwitch
          ref="tableColumnSwitch"
          :tableHead="tableHead"
          @doLayout="doLayout"
        />

        <!-- 表格查询组件 -->
        <tableSearch
          v-if="isSearch"
          :searchList="tableHeadSearchs"
          @show="handleTableSearch"
          @searchMethods="searchMethods"
          ref="tableSearch"
        />

        <!-- 表格导出组件 -->
        <tableExport
          v-if="isExport"
          @handleExport="handleTableExport"
          ref="tableExport"
        />
      </div>
    </div>
    <!-- 表格组件 -->

    <!-- v-watermark="{ text: $store.state.user.name, time: Date.now() }" -->
    <div id="baleTable">
      <el-table
        v-if="maxTableHeight || defalutMaxTableHeight"
        ref="table"
        stripe
        :data="tableData"
        v-loading="tableLoading"
        :max-height="maxTableHeight || defalutMaxTableHeight"
        :height="maxTableHeight || defalutMaxTableHeight"
        :show-summary="isTableSummary"
        :summary-method="handleGetSummaries"
        :span-method="spanMethod"
        :row-class-name="tableRowClass"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        highlight-current-row
        border
        @sort-change="tableSortChange"
        header-cell-class-name="tableHeader"
        :row-key="rowKey"
        :tree-props="treeProps"
        :default-expand-all="defaultExpandAll"
      >
        <el-table-column
          v-if="isTableSelect"
          :selectable="tableSelectDis"
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          v-for="(headItem, headIndex) in tableHeadShows"
          :key="headIndex"
          :prop="headItem.prop"
          :label="headItem.label"
          :min-width="headItem.width"
          :sortable="headItem.isSort ? 'custom' : false"
          :align="headItem.align || 'center'"
          :fixed="headItem.fixed || false"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <slot
              v-if="headItem.specialProp"
              :scope="scope"
              :name="headItem.specialProp"
            />
            <span v-else>{{ scope.row[headItem.prop] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 -->
    <el-pagination
      class="mt15"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.page"
      :total="page.total"
      :page-sizes="pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
  </div>
</template>

<script>
import tableColumnSwitch from "./tableColumnSwitch.vue";
import tableSearch from "./tableSearch.vue";
import tableExport from "./tableExport.vue";
export default {
  name: "baleTable",
  components: {
    tableColumnSwitch,
    tableSearch,
    tableExport,
  },
  props: {
    rowKey: {
      default: "id",
      type: String,
    },
    treeProps: {
      default: () => ({}),
      type: Object,
    },
    defaultExpandAll: {
      default: false,
      type: Boolean,
    },
    // 表格数据
    tableData: {
      default: () => {
        return [];
      },
      type: Array,
      required: true,
    },
    tableLoading: {
      type: Boolean,
      default: false,
    },
    // 表头信息
    tableHead: {
      default: () => {
        return [];
      },
      type: Array,
      required: true,
    },
    // 是否支持搜索
    isSearch: {
      type: Boolean,
      default: false,
    },
    // 是否是否支持导出
    isExport: {
      type: Boolean,
      default: false,
    },
    // 是否支持列表显示隐藏
    isColumnSwith: {
      type: Boolean,
      default: true,
    },
    // 表格是否开启多选
    isTableSelect: {
      type: Boolean,
      default: false,
    },
    // 表格是否开启行单击事件
    isTableRowClick: {
      type: Boolean,
      default: false,
    },
    // 表格是否开启合计
    isTableSummary: {
      type: Boolean,
      default: false,
    },
    handleGetSummaries: {
      type: Function,
      default: () => {
        return () => {};
      },
    },
    // 表格最大高度
    maxTableHeight: {
      type: Number,
    },
    page: {
      type: Object,
      default: () => {
        return {
          page: 1,
          size: 25,
          total: 0,
        };
      },
      required: true,
    },
    pageSizes: {
      type: Array,
      default: () => {
        return [25, 50];
      },
    },
    tableSelectDis: {
      type: Function,
      default: () => {
        return () => {};
      },
    },
    tableRowClass: {
      type: Function,
      default: () => {
        return () => {};
      },
    },
    spanMethod: {
      type: Function,
      default: () => {
        return () => {};
      },
    },
  },
  data() {
    return {
      defalutMaxTableHeight: 0,
      // tableLoading: false,
    };
  },
  computed: {
    tableHeadShows() {
      return this.tableHead.filter((v) => {
        return v.isShow;
      });
    },
    tableHeadSearchs() {
      return this.tableHead.filter((v) => {
        return v.isSearch;
      });
    },
    // defalutMaxTableHeight () {
    //   let heigiht = document.getElementsByClassName('el-main').offsetheight
    //   return heigiht
    // }
  },
  mounted() {
    let heigiht = document.getElementById("app").offsetHeight;
    this.defalutMaxTableHeight = heigiht - 212;
  },
  beforeUpdate() {},
  methods: {
    doLayout() {
      this.$refs["table"].doLayout();
    },
    clearSelection() {
      this.$refs["table"].clearSelection();
    },
    clear() {
      this.$refs["table"].clearSort();
      this.$refs["tableSearch"].reset();
      this.$refs["tableColumnSwitch"].reset();
    },

    handleSelectionChange(rows) {
      if (!this.isTableSelect) return;
      else this.$emit("handleSelectionChange", rows);
    },
    // tableSelectDis(row) {
    //   this.$emit("tableSelectDis", row);
    // },
    handleRowClick(row, column, event) {
      /*   console.log("2222", this.isTableRowClick); */
      if (!this.isTableRowClick) return;
      else {
        let params = {
          row,
          column,
          event,
        };
        this.$emit("handleRowClick", params);
      }
    },
    // getSummaries(param) {
    //   if (!this.isTableSummary) return;
    //   else ;
    // },
    handleSizeChange(val) {
      this.$emit("pageSizeChange", val);
    },
    handleCurrentChange(val) {
      this.$emit("handleCurrentChange", val);
    },
    handleTableSearch() {
      this.$emit("handleTableSearch");
    },
    searchMethods(val) {
      this.$emit("searchMethods", val);
    },
    tableSortChange(column) {
      let params = {};
      if (column.order === "descending") {
        params.sort_str = column.prop;
        params.sort_order = "desc";
      } else if (column.order === "ascending") {
        params.sort_str = column.prop;
        params.sort_order = "asc";
      }

      this.$emit("tableSortChange", params);
    },
    handleTableExport(type) {
      this.$emit("handleTableExport", type);
    },
    exportDownload(data, name) {
      this.$refs["tableExport"].click(data, name);
    },
  },
  updated() {
    this.doLayout();
  },
};
</script>

<style lang="less" scoped>
.baleTable {
  width: 100%;
  .baleTable-head {
    width: 100%;
    height: 84px;
    // min-width:900px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;

    .head-left {
    }
    .head-left :nth-child(1) {
      // margin: 0px !important;
    }
    .head-right {
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
  #baleTable {
    position: relative;
  }
}
</style>
